// pages/aduting/resversion/reversion_detail.js
var util = require('../../../utils/util.js');
var dataLists = new Array();
var wait=5;
var disLock = false;
var lockObj = '开锁';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    images: ['/images/off.png', '/images/ON.png'],
    lockObj:'开锁',
    disLock:true,
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        that.setData({
          userInfo: res.data,
          id: options.id,
        })

        util.getReq("/app/home/allApartmentInterface", {
          source: res.data.source,
          uid: res.data.user_id,

        }, function (datas) {
          
          if (datas.code == 0) { 
            that.setData({
              dataList: datas.params.list
            })

          } else {
            wx.showToast({
              title: datas.desc,
            })

            
          
          }

        })
      }, fail: function () {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },
  bindOpenSe: function (e) {
    var that = this;
    if (!e.currentTarget.dataset.primarykey){
      wx.showToast({
        title: '请勿频繁操作',
      })
      return false;
    }
   
    util.getReq("/app/home/fdwxOpenLock", {
      source: that.data.userInfo.source,
      uid: that.data.userInfo.user_id,
      'lock_no': e.currentTarget.dataset.lockno,
      'apartment_id': e.currentTarget.dataset.id
    }, function (datas) {
      
      var datals = that.data.dataList;
      var len = that.data.dataList.length;
      var obj;
      var os;
      for (var i = 0; i < len; i++) {
        if (datals[i].apartment_id == e.currentTarget.dataset.id) {
        
          that.limitFive(that, datals[i], i);
          break;
        }

      }
      if (datas.code == 0) {
       wx.showModal({
         title: '提示',
         icon: 'success',
         content: e.currentTarget.dataset.name +'开锁成功',
       })
      

      } else {
        wx.showModal({
          title: '错误',
          content: datas.desc,
          mode: true,
          showCancel: false,
          confirmColor: '#007aff',//确定文字的颜色
        })
        return false;
      }

    })
  },
  limitFive: function (that, obj,i){
  if (wait == 0) {
    wait = 5;
    disLock= null,
    lockObj= null;
    obj.primarykey = disLock;
    obj.orderBy = lockObj;
    var datals = that.data.dataList;
    datals.splice(i,1, obj);
    that.setData({
      dataList: datals

    })
  } else {
    disLock= false,
    lockObj=wait;
    obj.primarykey = disLock;
    obj.orderBy = lockObj;
    var datals = that.data.dataList;
    datals.splice(i,1, obj);
    that.setData({
      dataList: datals

    })
    wait--;
    setTimeout(function () { that.limitFive(that, obj,i) }, 1000);
  }
  
}

})